﻿using Newtonsoft.Json.Serialization;
using RestApp.UI.Core;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;

namespace RestApp.UI.Controllers
{
    public class UsersApiController : ApiController
    {
        public HttpResponseMessage Get(string userLogin)
        {
            IBusinessFactory factory = new RestApp.UI.Core.BusinessLogic();
            var items = factory.UsersLogic.GetAllFilteredByLoginCredentials(userLogin);
            var jsonItems = items.Select(t => new
                {
                    id = t.Id,
                    name = t.FirstName,
                    lastName = t.LastName,
                    middleName = t.MiddleName,
                })
                .ToList();

            var formatter = new JsonMediaTypeFormatter();
            var json = formatter.SerializerSettings;

            json.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
            json.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
            json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            json.Formatting = Newtonsoft.Json.Formatting.Indented;
            json.ContractResolver = new CamelCasePropertyNamesContractResolver();
            json.Culture = new CultureInfo("en-US");

            return Request.CreateResponse(HttpStatusCode.OK, jsonItems, formatter);
        }
    }
}
